//
//  DXHomePictureView.swift
//  新浪微博
//
//  Created by dix on 2017/2/7.
//  Copyright © 2017年 dix. All rights reserved.
//

import UIKit
import SDWebImage
//cell可重用标识符
private let pictureViewCellId = "pictureViewCellId"
  //个张图片之间的间距
private let itemMargin: CGFloat = 5
// 获取每张图片的宽高(正方形)
private let itemWH: CGFloat = (DXSCREENW - 2*10 - 2*itemMargin)/3
class DXHomePictureView: UICollectionView {
    //定义一个属性供外界赋值
    var pic_urls: [DXPictureModel]?{
        didSet{
        //获取配图的size
            let size = dealPictureViewSize(count: pic_urls?.count ?? 0)
            self.snp_makeConstraints { (make) in
                make.size.equalTo(size)
            }
            //获取到layout
            let layout = collectionViewLayout as! UICollectionViewFlowLayout

            if pic_urls?.count == 1 {
                layout.itemSize = size
            }else {
                layout.itemSize = CGSize(width: itemWH, height: itemWH)
            }
            // 因为itemSize的宽度和配图的宽度没有同步导致打印错误 需要解决该问题 需要重新刷新ui
            layoutIfNeeded()
            //刷新ui
            reloadData()
        }
    }
    
    func dealPictureViewSize(count: Int) -> CGSize{
        if count == 1 {
            //判断图片地址是否为nil
            if let picUrls = pic_urls?.first?.thumbnail_pic {
                if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: picUrls) {
                    //得到图片大小
                    var w = image.size.width
                    var h = image.size.height
                    if w < 80 {
                        w = 80
                    }
                    if h > 150 {
                        h = 150
                    }
                    return CGSize(width: w, height: h)
                }
            }
        }
        //获取每张图片的行数和列数
        let col = count == 4 ? 2 : count > 3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1)/3 + 1
        //计算配图的宽和高
        let w = CGFloat(col)*itemWH + CGFloat(col - 1) * itemMargin
        let h = CGFloat(row)*itemWH + CGFloat(row - 1) * itemMargin
        return CGSize(width: w, height: h)
    
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //自定义layout
        let picLayout = UICollectionViewFlowLayout()
        picLayout.itemSize = CGSize(width: itemWH, height: itemWH)
        //设置行间距和列间距
        picLayout.minimumLineSpacing = itemMargin
        picLayout.minimumInteritemSpacing = itemMargin
        super.init(frame: frame, collectionViewLayout: picLayout)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUpUI(){
        backgroundColor = DXRandomColor()
        dataSource = self
        //注册
        register(DXHomePictureViewCell.self, forCellWithReuseIdentifier: pictureViewCellId)
    
    }
}

extension DXHomePictureView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: pictureViewCellId, for: indexPath) as! DXHomePictureViewCell
        cell.pic_url = pic_urls![indexPath.item]
        return cell
    }

}
//自定义cell
class DXHomePictureViewCell: UICollectionViewCell {
    //提供属性供外界赋值
    var pic_url: DXPictureModel?{
        didSet{
            //赋值
            bgImageView.dx_setImage(urlStr: pic_url?.thumbnail_pic)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置shitu
    private func setUpUI(){
        //添加控件
        contentView.addSubview(bgImageView)
        //设置约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
    }
    private lazy var bgImageView: UIImageView = {
        let img = UIImageView(imgName: "avatar_default_big")
        //设置image的填充方式
        img.contentMode = .scaleAspectFill
        //超出部分需要裁剪
        img.clipsToBounds = true
        return img
    
    }()


}
